package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.CustomException;
import cn.wolfcode.p2p.base.query.BidRequestQuery;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.util.Constants;
import cn.wolfcode.p2p.util.JSONResult;
import cn.wolfcode.p2p.util.PageResult;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class BidRequestController {
        @Autowired
        private IBidRequestService bidRequestService;
        @Autowired
        private IUserInfoService userInfoService;
        @Autowired
        private IRealAuthService realAuthService;


        @RequestMapping("bidrequest_publishaudit_list")
        public String bidRequestPublishauditList(@ModelAttribute("qo")BidRequestQuery qo, Model model){
                qo.setState(Constants.BIDREQUEST_STATE_APPLY);
                PageResult pageResult = bidRequestService.selectForList(qo);
                model.addAttribute("pageResult",pageResult);
                return "/bidrequest/publish_audit";
        }

        @RequestMapping("borrow_info")
        public String borrowInfo(Model model,Long id){
                BidRequest bidRequest = bidRequestService.getById(id);
                UserInfo userInfo = userInfoService.getById(bidRequest.getCreateUser().getId());
                RealAuth realAuth = realAuthService.getById(userInfo.getRealAuthId());
                model.addAttribute("bidRequest",bidRequest);
                model.addAttribute("userInfo",userInfo);
                model.addAttribute("realAuth",realAuth);
                return "/bidrequest/borrow_info";
        }

        @RequestMapping("bidrequest_publishaudit")
        @ResponseBody
        public JSONResult bidRequestPublishAudit(Long id, String remark, Integer state, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date publishTime){
                JSONResult jsonResult = new JSONResult();
                try {
                        bidRequestService.publishAudit(id,remark,state,publishTime);
                    }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                    } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                 return jsonResult;
        }

        @RequestMapping("bidrequest_audit1_list")
        public String bidrequestAudit1List(@ModelAttribute("qo")BidRequestQuery qo, Model model){
                qo.setState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_1);
                PageResult pageResult = bidRequestService.selectForList(qo);
                model.addAttribute("pageResult",pageResult);
                return "/bidrequest/audit1";
        }

        /**
         * 一审的审核
         * @return
         */
        @RequestMapping("bidrequest_audit1")
        @ResponseBody
        public JSONResult bidrequestAudit1(Long id, String remark, Integer state){
                JSONResult jsonResult = new JSONResult();
                try {
                        bidRequestService.fullAudit1(id,remark,state);
                }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                return jsonResult;
        }


        @RequestMapping("bidrequest_audit2_list")
        public String bidrequestAudit2List(@ModelAttribute("qo")BidRequestQuery qo, Model model){
                qo.setState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_2);
                PageResult pageResult = bidRequestService.selectForList(qo);
                model.addAttribute("pageResult",pageResult);
                return "/bidrequest/audit2";
        }

        @RequestMapping("bidrequest_audit2")
        @ResponseBody
        public JSONResult bidrequestAudit2(Long id, String remark, Integer state){
                JSONResult jsonResult = new JSONResult();
                try {
                        bidRequestService.fullAudit2(id,remark,state);
                }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                return jsonResult;
        }


}
